/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.backends.jena;

import java.util.Map;

/**
 *
 * @author Laurent WOUTERS
 */
public class JenaRuleCondition extends JenaCustomBuiltin {
    protected xowl.lang.owl2.Expression expression;
    
    public JenaRuleCondition(JenaRepository repo, xowl.lang.owl2.Expression exp, Map<xowl.interpreter.backends.rdf.XOWLVariableNode, com.hp.hpl.jena.graph.Node> vars) {
        super(repo, vars);
        this.expression = exp;
    }
    
    public void addVariable(xowl.lang.actions.QueryVariable var, com.hp.hpl.jena.graph.Node node) { variables.put(var, node); }
    
    @Override public boolean bodyCall(com.hp.hpl.jena.graph.Node[] nodes, int length, com.hp.hpl.jena.reasoner.rulesys.RuleContext rc) {
        JenaQueryVariableEvaluator eval = getQueryEvaluator(rc);
        try {
            xowl.interpreter.impl.AggregatedEvaluator evaluator = getEvaluator();
            evaluator.addPart(eval);
            xowl.lang.runtime.Value value = evaluator.evaluate(evaluator, expression);
            evaluator.removePart(eval);
            boolean result = Boolean.parseBoolean(evaluator.toLiteral(value).getLexicalValue());
            return result;
        } catch (xowl.interpreter.XOWLException ex) { repository.getLog().error(JenaRepository.name, ex.getHeadline(), ex); }
        return false;
    }
    @Override public void headAction(com.hp.hpl.jena.graph.Node[] nodes, int length, com.hp.hpl.jena.reasoner.rulesys.RuleContext rc) { }
}
